package com.woniu.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author LXB
 * @Date 2023/8/19 19:47
 * @Description
 **/
@ConditionalOnMissingClass("org.springframework.cloud.gateway.config.GatewayAutoConfiguration")
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
@ConfigurationProperties(prefix = "swagger")
@Getter
@Setter
public class Swagger2Config {

    private String title = "";

    private String version = "v1.0";

    private String description = "";

    private String basePackage = "com.woniu.controller";

    @Value("${swagger.basic.enable:false}")
    private boolean enable;

    /**
     * 创建RestApi 并包扫描controller
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(title)
                        .version(version)
                        .description(description)
                        .build())
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                //不显示错误的接口地址
                .paths(PathSelectors.any())
                //错误路径不监控
//                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

}